#include "can_board.h" 
//感觉没写好
//这里只存在一个实例
static CANBoardInstance* board_can_instance;

static void CANDecode(CANInstance* can_instance)
{
    uint8_t* rx_buf = can_instance -> rx_buf;
    CANBoardInstance* board_can_instance = (CANBoardInstance* )can_instance -> id;
    memcpy(board_can_instance -> buf, rx_buf, 6);
    board_can_instance -> buf[6] = rx_buf[6] & 0b00000001;
    board_can_instance -> buf[7] = rx_buf[6] & 0b00000010;
    board_can_instance -> buf[8] = rx_buf[6] & 0b00000100;
    board_can_instance -> buf[9] = rx_buf[6] & 0b00001000;
    board_can_instance -> buf[10] = rx_buf[6] & 0b00010000;
    board_can_instance -> buf[11] = rx_buf[6] & 0b00100000;
    board_can_instance -> buf[12] = rx_buf[6] & 0b01000000;
    board_can_instance -> buf[13] = rx_buf[6] & 0b10000000;
    board_can_instance -> buf[14] = rx_buf[7] & 0b00000001;
    board_can_instance -> buf[15] = rx_buf[7] & 0b00000010;
    board_can_instance -> buf[16] = rx_buf[7] & 0b00000100;
}

CANBoardInstance* CANBoardInit(CAN_HandleTypeDef* CAN_Board_Handler)
{
    CAN_Init_Config_s config;
    config.CANx = CAN_Board_Handler;
    config.rx_id = 0x221;
    config.tx_id = 0x221;
    config.can_module_callback = CANDecode;
    board_can_instance -> can_instance = CANInit(&config);
    return board_can_instance;
}

void CANBoardSend()
{
    CANBoardInstance* instance = board_can_instance;
    memcpy(instance -> can_instance -> tx_buf, instance -> buf , 6);
    instance -> can_instance -> tx_buf[6] = ( instance -> buf[6] ) |
                                            ( instance -> buf[7] << 1 ) |
                                            ( instance -> buf[8] << 2 ) |
                                            ( instance -> buf[9] << 3 ) |
                                            ( instance -> buf[10] << 4 ) |
                                            ( instance -> buf[11] << 5 ) |
                                            ( instance -> buf[12] << 6 ) |
                                            ( instance -> buf[13] << 7 ) ;
    instance -> can_instance -> tx_buf[7] = ( instance -> buf[14] ) |
                                            ( instance -> buf[15] << 1 )|
                                            ( instance -> buf[16] << 2 );
    CANSend(instance -> can_instance, 1);
}   


